package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ethink.order.api.info.TocSaleReturnImportLogDetailInfo;
import com.ethink.order.api.request.TocSaleImportLogDetailAddRequest;
import com.ethink.order.api.request.TocSaleReturnImportLogDetailAddRequest;
import com.ethink.order.api.request.TocSaleReturnImportLogResultSettleRequest;
import com.ethink.order.common.entity.TocSaleImportLogDetail;
import com.ethink.order.common.entity.TocSaleReturnImportLogDetail;
import com.ethink.order.common.entity.TocSaleReturnImportLogDetailBak;
import com.ethink.order.common.mapper.TocSaleReturnImportLogDetailBakMapper;
import com.ethink.order.common.mapper.TocSaleReturnImportLogDetailMapper;
import com.ethink.order.service.TocSaleReturnImportLogDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * TOC销售退单导入日志明细 Service接口实现
 *
 * @author default
 * @date 2022-12-16
 */
@Slf4j
@Service
public class TocSaleReturnImportLogDetailServiceImpl implements TocSaleReturnImportLogDetailService {

    @Autowired
    private TocSaleReturnImportLogDetailMapper tocSaleReturnImportLogDetailMapper;

    @Autowired
    private TocSaleReturnImportLogDetailBakMapper bakMapper;

    @Override
    public void batchInsert(List<TocSaleReturnImportLogDetailAddRequest> item, Long id, Map<String, TocSaleReturnImportLogDetailInfo> detailInfoMap) {
        for (TocSaleReturnImportLogDetailAddRequest request : item) {
            TocSaleReturnImportLogDetailInfo detailInfo = detailInfoMap.get(request.getShopOrderNo());
            if (ObjectUtil.isEmpty(detailInfo)) {
                insert(request, id);
            } else {
                tocSaleReturnImportLogDetailMapper.update(null,
                        new LambdaUpdateWrapper<TocSaleReturnImportLogDetail>()
                                .eq(TocSaleReturnImportLogDetail::getId, detailInfo.getId())
                                .set(TocSaleReturnImportLogDetail::getShopReturnOrderNo, detailInfo.getShopReturnOrderNo())
                                .set(TocSaleReturnImportLogDetail::getShopOrderStatus, detailInfo.getShopOrderStatus())
                                .set(TocSaleReturnImportLogDetail::getShopShStatus, detailInfo.getShopShStatus()));

            }
            //备份
            TocSaleReturnImportLogDetailBak bak = BeanUtil.copyProperties(request, TocSaleReturnImportLogDetailBak.class);
            bak.setLogId(id);
            bakMapper.insert(bak);
        }
    }

    @Override
    public Integer insert(TocSaleReturnImportLogDetailAddRequest request, Long logId) {
        TocSaleReturnImportLogDetail tocSaleReturnImportLogDetail = BeanUtil.copyProperties(request, TocSaleReturnImportLogDetail.class);
        tocSaleReturnImportLogDetail.setLogId(logId);
        tocSaleReturnImportLogDetail.setReturnStatus(1);
        return tocSaleReturnImportLogDetailMapper.insert(tocSaleReturnImportLogDetail);
    }

    @Override
    public List<TocSaleReturnImportLogDetailInfo> queryDetailByShopOrderNoList(List<String> shopOrderNoList) {
        return tocSaleReturnImportLogDetailMapper.queryDetailByShopOrderNoList(shopOrderNoList);
    }

    @Override
    public List<TocSaleReturnImportLogDetailInfo> queryDetailListByLogId(Long id) {
        return tocSaleReturnImportLogDetailMapper.selectListByLogId(id);
    }

    @Override
    public void settleOrder(TocSaleReturnImportLogResultSettleRequest settleRequest) {
         tocSaleReturnImportLogDetailMapper.settleOrder(settleRequest);
    }

}
